{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0c424879",
   "metadata": {},
   "source": [
    "### 回译数据增强"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9702521b",
   "metadata": {},
   "source": [
    "一般基于Google接口，将文本数据翻译成另外一种语言（一般选择小语种），之后再翻译回原语言\n",
    "\n",
    "针对短文本回译重复率过高的问题，可以进行连续的多语言翻译，如中问->韩文->日语->英文->中文，根据经验，最多只采用3次连续翻译"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2177f43a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install googletrans-temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96dd4580",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_1 = \"这家酒店设施非常不错\"\n",
    "p_2 = \"这家酒店价格很便宜\"\n",
    "n_1 = \"拖鞋都发霉了，太差了\"\n",
    "n_2 = \"电视不好用，没有看到足球\"\n",
    "\n",
    "from googletrans import Translator\n",
    "\n",
    "translator = Translator()\n",
    "translations = translator.translate([p_1, p_2, n_1, n_2], dest='ko')  # korean\n",
    "ko_res = list(map(lambda x: x.text, translations))\n",
    "print(\"韩文翻译结果：\", ko_res)\n",
    "\n",
    "# 翻译回中文\n",
    "translations = translator.translaten(ko_res, dest='zh-cn')\n",
    "cn_res = list(map(lambda x: x.text, translations))\n",
    "print(\"回译得到的增强数据：\", cn_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87a7150d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torchX",
   "language": "python",
   "name": "torchx"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
